package com.qianfeng.myymatou.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.qianfeng.myymatou.R;
import com.qianfeng.myymatou.bean.HotSaleBean;
import com.qianfeng.myymatou.utils.LogUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/15.
 * <p/>
 * 组合控件
 * “特卖”界面-->“全球热卖”显示项
 * 包含三张图片
 */
public class HotSaleView extends LinearLayout {

    private static final String TAG = "HotSaleView";

    private Context mContext;
    private List<HotSaleBean.DataBean.BannersBean> mBannersList;
    private List<ImageView> mIvBannersList;
    private ImageView mIvHotSaleLeft;
    private ImageView mIvHotSaleRightAbove;
    private ImageView mIvHotSaleRightBelow;

    public HotSaleView(Context context) {
        this(context, null);
    }

    public HotSaleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HotSaleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView(context);
    }

    /**
     * 初始化控件
     * @param context 上下文
     */
    private void initView(Context context) {
        //初始化数据
        initData(context);

        //找控件
        findView();

        //初始化BannersList
        initBannersList();
    }

    /**
     * 初始化数据
     */
    private void initData(Context context) {
        mContext = context;
        mBannersList=new ArrayList<>();
        mIvBannersList=new ArrayList<>();
    }

    /**
     * 找控件
     */
    private void findView() {
        LayoutInflater.from(mContext).inflate(R.layout.view_hot_sale, this);
        mIvHotSaleLeft = (ImageView) findViewById(R.id.iv_hotSale_left);
        mIvHotSaleRightAbove = (ImageView) findViewById(R.id.iv_hotSale_rightAbove);
        mIvHotSaleRightBelow = (ImageView) findViewById(R.id.iv_hotSale_rightBelow);
    }

    /**
     * 初始化BannersList
     */
    private void initBannersList() {
        mIvBannersList.add(mIvHotSaleLeft);
        mIvBannersList.add(mIvHotSaleRightAbove);
        mIvBannersList.add(mIvHotSaleRightBelow);
    }

    /**
     * 设置数据，由外部调用，传入数据源
     *
     * @param bannersList 广告条数据源
     */
    public void setHotSaleData(List<HotSaleBean.DataBean.BannersBean> bannersList){
        if (bannersList!=null && bannersList.size()>0){
            mBannersList.addAll(bannersList);
            for (int i = 0; i < 3; i++) {
                LogUtils.i(TAG,"PicUrl-->"+mBannersList.get(i).getPicUrl());
                setImageByPicasso(mBannersList.get(i).getPicUrl(),mIvBannersList.get(i));

                //给广告条中每个图片做监听
                final int finalI = i;
                mIvBannersList.get(i).setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //TODO 你猜对了，这里还是缺跳转
                        Toast.makeText(mContext, "我是 "+ finalI +" 号！", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    }

    /**
     * 用Picasso设置图片到ImageView
     *
     * @param url 图片url
     * @param ivBanner 广告条中的ImageView
     */
    private void setImageByPicasso(String url, ImageView ivBanner) {
        Picasso.with(mContext)
                .load(url)
                .noFade()
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .into(ivBanner);
    }

}
